/**
 * create by young (young2f@outlook.com) on 2023/4/21
 * 数据库服务
 */
'use strict';


module.exports = class {
    constructor(app) {
        this.app = app;
        this.logger = app.logger;
        this.config = app.config;
    }

    async insert(model, data) {
        return this.app.model[model](data).save();
    }

    async insertMany(model, array) {
        return await this.app.model[model].insertMany(array);
    }
// const result = await ctx.model.Setting.findOneAndUpdate({ tenantCode }, body, { upsert: true });
    async findOne(model, query, sort) {
        if (!sort) sort = {lean: true};
        return await this.app.model[model].findOne(query, null, sort);
    }
    async findOneAndUpdate(model, query, body) {
        // if (!sort) sort = {lean: true};
        return await this.app.model[model].findOneAndUpdate(query, body, { upsert: true });
    }
    async find(model, query, sort) {
        if (!sort) sort = {lean: true};
        return await this.app.model[model].find(query, null, sort);
    }

    async updateOne(model, query,data) {
        return await this.app.model[model].updateOne(query,data);
    }
    async updateMany(model, query,data) {
        return await this.app.model[model].updateMany(query,data);
    }
    async aggregate(model, query) {
        return await this.app.model[model].aggregate(query);
    }

    async deleteOne(model, query) {
        return await this.app.model[model].deleteOne(query);
    }


    async deleteMany(model, query) {
        return await this.app.model[model].deleteMany(query);
    }

    async count(model, query) {
        return await this.app.model[model].countDocuments(query);
    }
};
